package com.erp.app.login.web;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.erp.app.admin.systemMenu.ejb.SystemMenuGroupPK;
import com.erp.app.common.ProjectConstants;
import com.erp.app.common.select.beans.SelectDelegate;
import com.erp.app.login.poEnquiry.criteria.PoEnquirySrchCriteria;
import com.erp.app.login.poEnquiry.vo.PoEnquiryVo;
import com.erp.app.login.taskEnquiry.criteria.TaskEnquirySrchCriteria;
import com.erp.app.login.taskEnquiry.vo.TaskEnquiryVo;
import com.erp.app.servlet.web.MenuIconServlet;
import com.erp.app.util.web.ActionErrorUtil;
import com.erp.framework.controller.action.SimpleSearchStyleAction;
import com.erp.framework.model.delegate.SimplePageEditStyleDelegate;
import com.erp.framework.model.pk.SimpleBasePk;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;
import com.erp.framework.util.mainmenu.MenuGroup;
import com.erp.framework.util.user.User;

public class MainMenuAction extends SimpleSearchStyleAction {

    public static final String actionChangeMenuTab = "changeMenuTab";
    public static final String actionChangeEnqTab = "changeEnqTab";
    public static final String actionViewMenuGroupImage = "viewMenuGroupImage";
    public static final String actionShowToDoList = "actionShowToDoList";
    public static final String actionShowAlertList = "actionShowAlertList";
    public static final String actionEnquiry = "actionEnquiry";
    public static final String actionSoEnquiry = "actionSoEnquiry";
    public static final String actionPoEnquiry = "actionPoEnquiry";
    public static final String actionTaskFabricEnquiry = "actionTaskFabricEnquiry";
    public static final String actionTaskStyleEnquiry = "actionTaskStyleEnquiry";
    public static final String actionEditDoc = "editDoc";
    public static final String actionChangeMenuMode = "actionChangeMenuMode";
    
    public static final String actionHandleOsTask = "handleOsTask";
    
    public static final String ALERT_LIST = "ALERT_LIST";
    public static final String TODO_LIST = "TODO_LIST";
    
    public static final String ACTION_EDIT_ALERT_TASK = "actionEditAlertTask";
    public static final String STATUS_EDIT_ALERT_TASK = ACTION_EDIT_ALERT_TASK;
    
    public ActionForward otherExecute(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response) throws ServletException {

        HttpSession session = request.getSession(false);
        ActionErrors errors = new ActionErrors();

        MainMenuForm form = (MainMenuForm) formIn;
        String action = form.getPageEvent();
        action = (action == null) ? "" : action.trim();

        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
        
        getRecordsFromDb(mapping, formIn, request, response, errors);
        // when exit other module, re-search
        if("exit".equals(form.getPageEvent())) {
        	form.setPageEventSrc("");
        	form.setOtherPageEvent("");
        	form.setOtherPageEventSrc("");
        	if(session.getAttribute(ProjectConstants.SESSION_TASK_ENQUIRY_TYPE) != null) {
            	session.removeAttribute(ProjectConstants.SESSION_TASK_ENQUIRY_TYPE);
            }
        }
        // set default task type to Fabric 
        if( CommonUtil.isEmpty(form.getPageEventSrc()) && CommonUtil.isEmpty(form.getOtherPageEvent()) 
                && CommonUtil.isEmpty(form.getOtherPageEventSrc()) ){
            form.setPageEventSrc(ProjectConstants.MOD_ENQ_MENU_TASK_FABRIC);
            if(session.getAttribute(ProjectConstants.SESSION_TASK_ENQUIRY_TYPE) != null) {
            	form.setPageEventSrc((String) session.getAttribute(ProjectConstants.SESSION_TASK_ENQUIRY_TYPE));
            }
            actionForward = processChangeEnqTab(mapping, formIn, request, response, errors);
        }
        //set default to SO enquiry
        /*
        if( CommonUtil.isEmpty(form.getPageEventSrc()) && CommonUtil.isEmpty(form.getOtherPageEvent()) 
                && CommonUtil.isEmpty(form.getOtherPageEventSrc()) ){
            form.setPageEventSrc(ProjectConstants.MOD_ENQ_MENU_SO);
            actionForward = processChangeEnqTab(mapping, formIn, request, response, errors);
        }
        */
        
        //use session to pass variables for now... must debug why form cannot pass pageEvent later.
        /*
        String sessionMenuMode = (String)session.getAttribute("SESSION_MENU_MODE");
        if(!CommonUtil.isEmpty(sessionMenuMode)){
            form.setMenuMode(sessionMenuMode);
        }
        */
        
        if (CommonUtil.isEmpty(form.getSelectedMenu())) {
            String defaultMenu = chooseDefaultMenu(editingUser);
            if (defaultMenu != null) {
                form.setPageEventSrc(defaultMenu);
                actionForward = processChangeMenuTab(mapping, formIn, request, response, errors);
                setHandled(request);
            } else {
                // logout? show warning when no group can be selected?
            }

        }
        
        //IMPORTANT: DO NOT USE ELSE IN ANY OF THE FOLLOWING

        if (!isHandled(request) && action.equalsIgnoreCase(actionChangeMenuTab)) {
            actionForward = processChangeMenuTab(mapping, formIn, request, response, errors);
            setHandled(request);
        }

        if (!isHandled(request) && action.equalsIgnoreCase(actionChangeEnqTab)) {
            actionForward = processChangeEnqTab(mapping, formIn, request, response, errors);
            setHandled(request);
        }

        if (!isHandled(request) && action.equalsIgnoreCase(actionShowToDoList)) {
            form.setShowSideList(TODO_LIST);
            setHandled(request);
        }

        if (!isHandled(request) && action.equalsIgnoreCase(actionShowAlertList)) {
            form.setShowSideList(ALERT_LIST);
            setHandled(request);
        }

        // Action: display Menu Group Image
        if (!isHandled(request) && action.equalsIgnoreCase(actionViewMenuGroupImage)) {
            actionForward = processViewMenuGroupImage(mapping, formIn, request, response, errors);
            setHandled(request);
        }
        
        if (!isHandled(request) && action.equalsIgnoreCase(actionEnquiry)) {
            actionForward = processActionEnquiry(mapping, formIn, request, response, errors);
            setHandled(request);
        }        
        
        if (!isHandled(request) && action.equalsIgnoreCase(actionChangeMenuMode)) {
            processActionChangeMenuMode(mapping, formIn, request, response, errors);
            setHandled(request);
        }        
        
        if (!isHandled(request) && action.equalsIgnoreCase(actionHandleOsTask)) {
            actionForward = processActionHandleOsTask(mapping, formIn, request, response, errors);
            setHandled(request);
        }
        
        if (!isHandled(request) && action.equalsIgnoreCase(ACTION_EDIT_ALERT_TASK)) {
            actionForward = processActionEditAlertTask(mapping, formIn, request, response, errors);
            setHandled(request);
        }
        
        saveErrors(request, errors);
        saveToken(request);
        return actionForward;
    }
    
    public ActionForward processActionHandleOsTask(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        MainMenuForm form = (MainMenuForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
        try {
        	String action = form.getPageEvent();
        	String selectedDateStr = form.getPageEventSrc();
        	selectedDateStr =  selectedDateStr.substring(4, 6) + "/" + selectedDateStr.substring(6, 8) + "/" + selectedDateStr.substring(2, 4);
//        		CommonUtil.dateToString(CommonUtil.stringToDate(selectedDateStr, "yyyymmdd"));
			ArrayList osTaskCrit = new ArrayList();
			osTaskCrit.add(selectedDateStr);
		
			
			session.setAttribute(ProjectConstants.SESSION_OS_TASK_DEFAULT_CRIT, osTaskCrit);

        }catch(Exception e){
        	Logger.logExceptions(this, e);        	
        }
        
        return actionForward;
    }

    public ActionForward processViewMenuGroupImage(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {

        HttpSession session = request.getSession(false);
        MainMenuForm form = (MainMenuForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        byte[] content = null;
        try {
            ArrayList menuList = form.getMenuGroupList();
            String selected = form.getSelectedMenu();
            for (Iterator it = menuList.iterator(); it.hasNext();) {
                MenuGroup menuGroup = (MenuGroup) it.next();
                if (menuGroup.getMenuId().equals(selected)) {
                    content = menuGroup.getMenuImage();
                    break;
                }
            }

            response.setContentType("image/gif");
            response.setStatus(response.SC_OK);
            OutputStream out = response.getOutputStream();
            if (content != null) {
                out.write(content);
            }
            out.close();
            actionForward = null;
        } catch (Exception e) {
            errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("exception.general", e.getMessage()));
            Logger.logExceptions(this, e);
        }

        return actionForward;
    }    
    
    public void getRecordsFromDb(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {

        MainMenuForm form = (MainMenuForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());
        HttpSession session = request.getSession(false);

        try {
            User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
            String eventSrc = form.getPageEventSrc();
            
            ArrayList list;
            
            // for task fabric
            TaskEnquirySrchCriteria taskFabricEnqCrit = new TaskEnquirySrchCriteria();
            taskFabricEnqCrit.setSrchCompanyCode(editingUser.getCompanyCode());
            taskFabricEnqCrit.setSrchUser(editingUser);
            taskFabricEnqCrit.setSrchUserId(editingUser.getUserId());
            taskFabricEnqCrit.setSrchTaskType(ProjectConstants.CD_TASK_ENQUIRY_TYPE_FABRIC);
            SimplePageEditStyleDelegate dg = new SimplePageEditStyleDelegate();
            Collection taskEnqList = dg.findByCriteria(new TaskEnquiryVo(), taskFabricEnqCrit);
            list = new ArrayList();
            list.addAll(taskEnqList);
            form.setTaskFabricEnquiryList(list);
            Logger.debug(this, "getRecordsFromDb(), size of taskFabricEnquiryList=" + form.getTaskFabricEnquiryList().size());
            
            // for task style
            TaskEnquirySrchCriteria taskStyleEnqCrit = new TaskEnquirySrchCriteria();
            taskStyleEnqCrit.setSrchCompanyCode(editingUser.getCompanyCode());
            taskStyleEnqCrit.setSrchUser(editingUser);
            taskStyleEnqCrit.setSrchTaskType(ProjectConstants.CD_TASK_ENQUIRY_TYPE_STYLE);
            taskStyleEnqCrit.setSrchUserId(editingUser.getUserId());
            dg = new SimplePageEditStyleDelegate();
            Collection taskStyleList = dg.findByCriteria(new TaskEnquiryVo(), taskStyleEnqCrit);
            list = new ArrayList();
            list.addAll(taskStyleList);
            form.setTaskStyleEnquiryList(list);
            Logger.debug(this, "getRecordsFromDb(), size of taskStyleEnquiryList=" + form.getTaskStyleEnquiryList().size());
            
            
            /*
            TaskAlertSrchCriteria alertCrit = new TaskAlertSrchCriteria();
            alertCrit.setSrchCompanyCode(editingUser.getCompanyCode());
            SimplePageEditStyleDelegate dg = new SimplePageEditStyleDelegate();            
            Collection alertList = dg.findByCriteria(new TaskAlertVo(), alertCrit);
            list = new ArrayList();
            list.addAll(alertList);
            form.setAlertList(list);
            Logger.debug(this, "getRecordsFromDb(), size of alertList=" + form.getAlertList().size());
            
            TodoListSrchCriteria todoCrit = new TodoListSrchCriteria();
            todoCrit.setSrchCompanyCode(editingUser.getCompanyCode());
            Collection todoList = dg.findByCriteria(new TodoListVo(), todoCrit);
            list = new ArrayList();
            list.addAll(todoList);
            form.setTodoList(list);
            Logger.debug(this, "getRecordsFromDb(), size of todoList=" + form.getTodoList().size()); 
            */
            
            // don't search so/po on default
            //SoEnquirySrchCriteria soEnqCrit = new SoEnquirySrchCriteria();
            //soEnqCrit.setSrchCompanyCode(editingUser.getCompanyCode());
            //soEnqCrit.setSrchUser(editingUser);
            //Collection soEnqList = dg.findByCriteria(new SoEnquiryVo(), soEnqCrit);
            list = new ArrayList();
            //list.addAll(soEnqList);
            form.setSoEnquiryList(list);
            Logger.debug(this, "getRecordsFromDb(), size of soEnqList=" + form.getSoEnquiryList().size()); 
                        
            //PoEnquirySrchCriteria poEnqCrit = new PoEnquirySrchCriteria();
            //poEnqCrit.setSrchCompanyCode(editingUser.getCompanyCode());
            //Collection poEnqList = dg.findByCriteria(new PoEnquiryVo(), poEnqCrit);
            list = new ArrayList();
            //list.addAll(poEnqList);
            form.setPoEnquiryList(list);
            Logger.debug(this, "getRecordsFromDb(), size of poEnqList=" + form.getPoEnquiryList().size()); 
            
        } catch (Exception e) {
            errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("exception.general", e.getMessage()));
            Logger.logExceptions(this, e);
        }
    }
    
    public void processActionChangeMenuMode(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {

        MainMenuForm form = (MainMenuForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());

        try {
            String pageEventSrc = form.getPageEventSrc();
            form.setMenuMode(pageEventSrc);
            Logger.debug(this, "processActionChangeMenuMode(), Menu Mode is set to: " + form.getMenuMode());
        }catch (Exception e) {
            errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("exception.general", e.getMessage()));
            Logger.logExceptions(this, e);
        }
    }
    
    public ActionForward processActionEditAlertTask(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {

        MainMenuForm form = (MainMenuForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());

        try {
            HttpSession session = request.getSession(false);
            User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
            

          
            
           
        } catch (Exception e) {
            errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("exception.general", e.getMessage()));
            Logger.logExceptions(this, e);
        }
        
        return actionForward;
    }
    

    public ActionForward processActionEnquiry(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {

        MainMenuForm form = (MainMenuForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());

        try {
            HttpSession session = request.getSession(false);
            User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
            SimplePageEditStyleDelegate dg = new SimplePageEditStyleDelegate();
            
            String otherPageEvent = form.getOtherPageEvent();
            Logger.debug(this, "processActionEnquiry(), otherPageEvent=" + otherPageEvent);            
            String otherPageEventSrc = form.getOtherPageEventSrc();
            Logger.debug(this, "processActionEnquiry(), otherPageEventSrc=" + otherPageEventSrc);            
            
            if(actionSoEnquiry.equals(otherPageEvent)){
                if("clear".equals(otherPageEventSrc)){
                    form.setSrchSoNo(null);
                    form.setSrchSoDate(null);
                    form.setSoEnquiryList(new ArrayList());
                    form.setResultList(new ArrayList());
                    return actionForward;
                }
                
                String soNo = form.getSrchSoNo();
                String soDate = form.getSrchSoDate();
                TaskEnquirySrchCriteria soEnqCrit = new TaskEnquirySrchCriteria();
                soEnqCrit.setSrchCompanyCode(editingUser.getCompanyCode());
                soEnqCrit.setSrchUser(editingUser);
                /*soEnqCrit.setSrchSoNoFr(soNo);
                soEnqCrit.setSrchSoNoTo(soNo);*/
                if( (!CommonUtil.isEmpty(soDate)) && (!CommonUtil.isValidDate(soDate)) ){
                    ActionErrorUtil.addErrorForInvalidDate(errors, "Search By Date");
                } else {
                    if(CommonUtil.isEmpty(soDate)){
                      /*  soEnqCrit.setSrchSoDateFr(null);
                        soEnqCrit.setSrchSoDateTo(null);*/
                    } else {
                       /* soEnqCrit.setSrchSoDateFr(soDate);
                        soEnqCrit.setSrchSoDateTo(soDate);*/
                    }                    
                }
                Collection soEnqList = dg.findByCriteria(new TaskEnquiryVo(), soEnqCrit);
                ArrayList list = new ArrayList();
                list.addAll(soEnqList);
                form.setSoEnquiryList(list);       
                form.setResultList(list);
                form.setSidx(0);                
                Logger.debug(this, "processActionEnquiry(), size of soEnqList=" + form.getSoEnquiryList().size());
                Logger.debug(this, "processActionEnquiry(), size of resultList=" + form.getResultList().size());
            } else if(actionPoEnquiry.equals(otherPageEvent)){
                if("clear".equals(otherPageEventSrc)){
                    form.setSrchPoNo(null);
                    form.setSrchPoDate(null);
                    form.setPoEnquiryList(new ArrayList());
                    form.setResultList(new ArrayList());
                    return actionForward;
                }
                String poNo = form.getSrchPoNo();
                String poDate = form.getSrchPoDate();
                PoEnquirySrchCriteria poEnqCrit = new PoEnquirySrchCriteria();
                poEnqCrit.setSrchCompanyCode(editingUser.getCompanyCode());
                poEnqCrit.setSrchPoNoFr(poNo);
                poEnqCrit.setSrchPoNoTo(poNo);                
                if( (!CommonUtil.isEmpty(poDate)) && (!CommonUtil.isValidDate(poDate)) ){
                    ActionErrorUtil.addErrorForInvalidDate(errors, "Search By Date");
                } else {                    
                    if(CommonUtil.isEmpty(poDate)){
                        poEnqCrit.setSrchPoDateFr(null);
                        poEnqCrit.setSrchPoDateTo(null);
                    } else {
                        poEnqCrit.setSrchPoDateFr(poDate);
                        poEnqCrit.setSrchPoDateTo(poDate);
                    }
                }
                Collection poEnqList = dg.findByCriteria(new PoEnquiryVo(), poEnqCrit);
                ArrayList list = new ArrayList();
                list.addAll(poEnqList);
                form.setPoEnquiryList(list);
                form.setResultList(list);
                form.setSidx(0);
                Logger.debug(this, "processActionEnquiry(), size of poEnqList=" + form.getPoEnquiryList().size());
                Logger.debug(this, "processActionEnquiry(), size of resultList=" + form.getResultList().size());
            } else if(actionTaskFabricEnquiry.equals(otherPageEvent)){
                if("clear".equals(otherPageEventSrc)){
                	form.setSrchTaskProjNo(null);
                    form.setTaskFabricEnquiryList(new ArrayList());
                    form.setResultList(new ArrayList());
                    return actionForward;
                }
                String projNo = form.getSrchTaskProjNo();
                TaskEnquirySrchCriteria taskFabricEnqCrit = new TaskEnquirySrchCriteria();
                taskFabricEnqCrit.setSrchCompanyCode(editingUser.getCompanyCode());
                taskFabricEnqCrit.setSrchProjNoFr(projNo);
                taskFabricEnqCrit.setSrchProjNoTo(projNo);
                taskFabricEnqCrit.setSrchTaskType(ProjectConstants.CD_TASK_ENQUIRY_TYPE_FABRIC);
                Collection taskFabricEnqList = dg.findByCriteria(new TaskEnquiryVo(), taskFabricEnqCrit);
                ArrayList list = new ArrayList();
                list.addAll(taskFabricEnqList);
                form.setTaskFabricEnquiryList(list);
                form.setResultList(list);
                form.setSidx(0);
                Logger.debug(this, "processActionEnquiry(), size of taskFabricEnqList=" + form.getTaskFabricEnquiryList().size());
                Logger.debug(this, "processActionEnquiry(), size of resultList=" + form.getResultList().size());
            } else if(actionTaskStyleEnquiry.equals(otherPageEvent)){
                if("clear".equals(otherPageEventSrc)){
                	form.setSrchTaskProjNo(null);
                    form.setTaskStyleEnquiryList(new ArrayList());
                    form.setResultList(new ArrayList());
                    return actionForward;
                }
                String projNo = form.getSrchTaskProjNo();
                TaskEnquirySrchCriteria taskStyleEnqCrit = new TaskEnquirySrchCriteria();
                taskStyleEnqCrit.setSrchCompanyCode(editingUser.getCompanyCode());
                taskStyleEnqCrit.setSrchProjNoFr(projNo);
                taskStyleEnqCrit.setSrchProjNoTo(projNo);
                taskStyleEnqCrit.setSrchTaskType(ProjectConstants.CD_TASK_ENQUIRY_TYPE_STYLE);
                Collection taskStyleEnqList = dg.findByCriteria(new TaskEnquiryVo(), taskStyleEnqCrit);
                ArrayList list = new ArrayList();
                list.addAll(taskStyleEnqList);
                form.setTaskStyleEnquiryList(list);
                form.setResultList(list);
                form.setSidx(0);
                Logger.debug(this, "processActionEnquiry(), size of taskStyleEnqList=" + form.getTaskStyleEnquiryList().size());
                Logger.debug(this, "processActionEnquiry(), size of resultList=" + form.getResultList().size());
            } else if (ACTION_SCROLL_LEFT.equals(otherPageEvent)) {
                setHandled(request);
                //actionForward = processActionScrollLeft(mapping, formIn, request, response, errors);
                processActionScrollLeft(mapping, formIn, request, response, errors);
                saveToken(request);
            } else if (ACTION_SCROLL_RIGHT.equals(otherPageEvent)) {
                setHandled(request);
                //actionForward = processActionScrollRight(mapping, formIn, request, response, errors);
                processActionScrollRight(mapping, formIn, request, response, errors);
                saveToken(request);
            } else if (actionEditDoc.equals(otherPageEvent)){
                String viewDocType = form.getEditDocType();
                String docNo = form.getEditDocNo();
                String companyCode = editingUser.getCompanyCode();
                String projNo = form.getEditTaskProjNo();
                String taskLineNo = form.getEditTaskLineNo();
                String styleLineNo = form.getEditStyleLineNo();
                
                
                if (viewDocType.equals(ProjectConstants.MOD_PROJ_FABRIC_TASK)) {
                	String pageEventSrc = "&popupProjNo="+projNo
					+"&popupFabricLineNo="+styleLineNo
					+"&popupTaskLineNo="+taskLineNo+ "&isReadOnly=NO";
                	form.setEditTaskEnquiryEventSrc(pageEventSrc);
                	if(session.getAttribute(ProjectConstants.SESSION_TASK_ENQUIRY_TYPE) != null) {
                		session.removeAttribute(ProjectConstants.SESSION_TASK_ENQUIRY_TYPE);
                	}
                	session.setAttribute(ProjectConstants.SESSION_TASK_ENQUIRY_TYPE, ProjectConstants.MOD_ENQ_MENU_TASK_FABRIC);
                } else if (viewDocType.equals(ProjectConstants.MOD_PROJ_STYLE_TASK)) {
                	String pageEventSrc = "&popupProjNo="+projNo
					+"&popupStyleLineNo="+styleLineNo
					+"&popupTaskLineNo="+taskLineNo+ "&isReadOnly=NO";
                	if(session.getAttribute(ProjectConstants.SESSION_TASK_ENQUIRY_TYPE) != null) {
                		session.removeAttribute(ProjectConstants.SESSION_TASK_ENQUIRY_TYPE);
                	}
                	session.setAttribute(ProjectConstants.SESSION_TASK_ENQUIRY_TYPE, ProjectConstants.MOD_ENQ_MENU_TASK_STYLE);
                	form.setEditTaskEnquiryEventSrc(pageEventSrc);
                }
            }
            
        } catch (Exception e) {
            errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("exception.general", e.getMessage()));
            Logger.logExceptions(this, e);
        }
        
        return actionForward;
    }
    
    public ActionForward processChangeEnqTab(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {

        MainMenuForm form = (MainMenuForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());

        try {
            String eventSrc = form.getPageEventSrc();
            if (!CommonUtil.isEmpty(eventSrc)) {
                form.setSelectedEnqMenu(eventSrc);
                
                if(ProjectConstants.MOD_ENQ_MENU_SO.equals(eventSrc)){
                    form.setResultList(form.getSoEnquiryList());
                    form.setSidx(0);
                } else if(ProjectConstants.MOD_ENQ_MENU_PO.equals(eventSrc)){
                    form.setResultList(form.getPoEnquiryList());
                    form.setSidx(0);
                } else if(ProjectConstants.MOD_ENQ_MENU_TASK_STYLE.equals(eventSrc)) {
                	form.setResultList(form.getTaskStyleEnquiryList());
                    form.setSidx(0);
                } else if(ProjectConstants.MOD_ENQ_MENU_TASK_FABRIC.equals(eventSrc)) {
                	form.setResultList(form.getTaskFabricEnquiryList());
                    form.setSidx(0);
                }
            }
            
            String otherPageEvent = form.getOtherPageEvent();
            Logger.debug(this, "processChangeEnqTab(), otherPageEvent=" + otherPageEvent);
            
            String otherPageEventSrc = form.getOtherPageEventSrc();
            Logger.debug(this, "processChangeEnqTab(), otherPageEventSrc=" + otherPageEventSrc);
            
        } catch (Exception e) {
            errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("exception.general", e.getMessage()));
            Logger.logExceptions(this, e);
        }

        return actionForward;
    }

    public ActionForward processChangeMenuTab(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors) {
        HttpSession session = request.getSession(false);
        MainMenuForm form = (MainMenuForm) formIn;
        ActionForward actionForward = new ActionForward(mapping.getInput());
        User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);

        try {
            String eventSrc = form.getPageEventSrc();
            if (!CommonUtil.isEmpty(eventSrc)) {
                ArrayList menuList = loadMenuList(editingUser, eventSrc);

                // for displaying icons in MenuIconServlet
                if (menuList != null) {
                    HashMap map = (HashMap) session.getAttribute(MenuIconServlet.ICON_STORE_SESSION_NAME);
                    if (map == null) {
                        map = new HashMap();
                    }

                    for (Iterator it = menuList.iterator(); it.hasNext();) {
                        MenuGroup menuGroup = (MenuGroup) it.next();

                        SystemMenuGroupPK pk = new SystemMenuGroupPK(menuGroup.getCompanyCode(), menuGroup.getGroupId(),
                                menuGroup.getMenuId());

                        if (!map.containsKey(pk)) {
                            map.put(pk, menuGroup.getMenuIcon());
                        }
                    }

                    session.setAttribute(MenuIconServlet.ICON_STORE_SESSION_NAME, map);
                }

                form.setMenuList(menuList);
                form.setSelectedMenu(eventSrc);
            }
        } catch (Exception e) {
            errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("exception.general", e.getMessage()));
            Logger.logExceptions(this, e);
        }

        return actionForward;
    }

    String[] listToArray(ArrayList alist) {
        String[] resultArray = new String[alist.size()];
        for (int i = 0; i < alist.size(); i++) {
            resultArray[i] = (String) alist.get(i);
        }

        return resultArray;
    }

    protected String chooseDefaultMenu(User editingUser) {
        String moduleName = null;

        try {
            SelectDelegate dg = new SelectDelegate();
            ArrayList resultList = dg.findAllMenuIds(editingUser.getRoleGroupId(), editingUser.getCompanyCode());
            String[] menuChoice = listToArray(resultList);

            if (Logger.isDebugLevel()) {
                for (int i = 0; i < menuChoice.length; i++) {
                    Logger.debug(this, "chooseDefaultMenu(), menuChoice[" + i + "]=" + menuChoice[i]);
                }
            }

            ArrayList menuList = null;
            int i = 0;

            while (menuList == null || menuList.size() == 0) {
                moduleName = menuChoice[i++];
                menuList = loadMenuList(editingUser, moduleName);
            }
            Logger.debug(this, "chooseDefaultMenu(), chosenMenuName = " + moduleName);
            if (menuList == null || menuList.size() == 0) {
                moduleName = null;
            }

        } catch (Exception e) {
            Logger.error(this, "chooseDefaultMenu(), error: " + e.getMessage());
            e.printStackTrace();
            Logger.logExceptions(this, e);
        }
        return moduleName;
    }

    protected ArrayList loadMenuList(User editingUser, String moduleName) {

        ArrayList menuList = null;
        MenuGroup menuGroup = new MenuGroup();

        if (!CommonUtil.isEmpty(moduleName)) {
            menuList = menuGroup.getMenuGroupList(editingUser.getCompanyCode(), editingUser.getUserId(), moduleName);
        }

        if (menuList == null) {
            menuList = new ArrayList();
        }

        return menuList;
    }

    protected String getModuleName(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response) {
        return ProjectConstants.MOD_MAIN_MENU;
    }
    
    protected String getFunctionId(){return null;}
    
    protected void setFormType(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors){}
    
    protected String getMasterFormSessionName(){return null;}
        
    protected Class getMasterFormClass(){return null;}
    
    protected Class getMasterVOClass(){return null;}
    
    protected Class getSearchCriteriaClass(){return null;}
        
    protected void setOtherCriteria(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors, Object criteria){}
    
    protected SimpleBasePk getPk(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
            HttpServletResponse response, ActionErrors errors){return null;}
}
